package com.test39_集合应用;

import com.test39_集合应用.factory.ServiceFactory;
import com.test39_集合应用.service.IDataService;
import com.test39_集合应用.vo.Privilege;
import com.test39_集合应用.vo.Role;
import com.test39_集合应用.vo.User;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 类集序列化对象存储
 */
public class YootkDemo_01 {
    private static IDataService dataService;
    static {
        dataService = ServiceFactory.getDataServiceInstance();
        try {
            dataService.store(init());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test1() throws Exception {
        System.out.println("------------------- 根据用户查找对应信息 -------------------");
        dataService.showByUser();
    }

    @Test
    public void test2() throws Exception {
        System.out.println("------------------- 根据角色查找对应信息 -------------------");
        dataService.showByRole();
    }

    @Test
    public void test3() throws Exception {
        System.out.println("------------------- 根据权限查找对应信息 -------------------");
        dataService.showByPrivilege();
    }

    public static Map<String, Object> init() {
        Map<String, Object> map = new HashMap<>();
        User userA = new User("yootk-zs", "张三");
        User userB = new User("yootk-ls", "李四");
        User userC = new User("yootk-ww", "王五");
        Role roleA = new Role("user", "用户管理");
        Role roleB = new Role("backup", "系统备份");
        Privilege priA = new Privilege("user:add", "用户增加");
        Privilege priB = new Privilege("user:edit", "用户修改");
        Privilege priC = new Privilege("user:delete", "用户删除");
        Privilege priD = new Privilege("user:select", "用户查询");
        Privilege priE = new Privilege("backup:all", "全量备份");
        Privilege priF = new Privilege("backup:inc", "增量备份");
        Privilege priG = new Privilege("backup:recover", "备份恢复");
        // 配置角色与权限关联
        roleA.getPrivileges().add(priA);
        roleA.getPrivileges().add(priB);
        roleA.getPrivileges().add(priC);
        roleA.getPrivileges().add(priD);
        roleB.getPrivileges().add(priE);
        roleB.getPrivileges().add(priF);
        roleB.getPrivileges().add(priG);
        // 配置权限与角色关联
        priA.setRole(roleA);
        priB.setRole(roleA);
        priC.setRole(roleA);
        priD.setRole(roleA);
        priE.setRole(roleB);
        priF.setRole(roleB);
        priG.setRole(roleB);
        // 配置用户与角色关联
        userA.getRoles().add(roleA);
        userA.getRoles().add(roleB);
        userB.getRoles().add(roleA);
        userC.getRoles().add(roleB);
        // 配置角色与用户关联
        roleA.getUsers().add(userA);
        roleA.getUsers().add(userB);
        roleB.getUsers().add(userA);
        roleB.getUsers().add(userC);

        // 保存用户信息
        map.put(userA.getUid(), userA);
        map.put(userB.getUid(), userB);
        map.put(userC.getUid(), userC);
        // 保存角色信息
        map.put(roleA.getRid(), roleA);
        map.put(roleB.getRid(), roleB);
        // 保存权限信息
        map.put(priA.getPid(), priA);
        map.put(priB.getPid(), priB);
        map.put(priC.getPid(), priC);
        map.put(priD.getPid(), priD);
        map.put(priE.getPid(), priE);
        map.put(priF.getPid(), priF);
        map.put(priG.getPid(), priG);
        return map;
    }
}
